#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

bool compare(vector<int> A,vector<int> B){
    if(A[0]==B[0]){
        
        return A[1]<B[1];
    }
    return A[0]<B[0];
}

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        sort(intervals.begin(),intervals.end(),compare);
        vector<vector<int>> result;
        vector<int> temp;
        for(int i = 0;i<intervals.size();++i){
            if(i==0){
                temp = intervals[0];
            }
            else{
                if(func(temp,intervals[i])){
                    temp = {min(temp[0],intervals[i][0]),max(temp[1],intervals[i][1])};
                }
                else{
                    result.push_back(temp);
                    temp = intervals[i];
                }
            }
        }
        result.push_back(temp);
        return result;
    }

    //判断是否可以合并
    bool func(vector<int>& A,vector<int>& B){
        if(A[1]<B[0]){
            return false;
            
        }
        return true;
    }
};


int main(){
    Solution S;
    vector<vector<int>> intervals = {{1,3},{2,6},{8,10},{15,18}};
    vector<vector<int>> result = S.merge(intervals);
    return 0;
}